#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence
@file: util.py
@time: 2017/12/20 20:52
"""
import hashlib
import json
from datetime import datetime


def get_now_time():
    return datetime.now().strftime("%Y-%m-%d %H:%M:%S")


def get_now_date():
    return datetime.now().strftime("%Y-%m-%d")


# 把字符串转成datetime
def str_to_date(string, fmt="%Y-%m-%d"):
    return datetime.strptime(string, fmt)


# 获取之前某天
def get_pre_day(date_str, delay, fmt="%Y-%m-%d"):
    import datetime
    _date = str_to_date(date_str, fmt)
    one_day = datetime.timedelta(days=delay)
    day = _date - one_day
    return day.strftime(fmt)


# 生成objectID 接口
# def gen_id(pripid, enttype, s_ext_nodenum):
#     string = str(pripid) + "|" + str(enttype) + "|" + str(s_ext_nodenum)
#     return get_md5(string)


def get_md5(string):
    m2 = hashlib.md5()
    m2.update(string)
    return m2.hexdigest().lower()


# 生成 省份与企业联合主键md5
def gen_schedule_key_md5(table_name, company):
    hash_key = table_name + '#'
    hash_key += company + '#'

    md5_obj = hashlib.md5()
    md5_obj.update(hash_key)
    company_md5 = md5_obj.hexdigest().lower()
    return company_md5, hash_key


def json_loads(text):
    try:
        return json.loads(text)
    except:
        return None


def get_time_stamp():
    import time
    return str(int(time.time() * 1000))


# 随机生成9位数字令牌
def create_nine_num():
    import random
    li = ['10', '11']
    num = random.choice(li)
    for i in range(0, 7):
        num += str(random.randint(0, 9))
    return num
